//
//  XYMainViewController.swift
//  WeiBo
//
//  Created by 陈修宇 on 16/3/3.
//  Copyright © 2016年 陈修宇. All rights reserved.
//

import UIKit

class XYMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 统一设置字体颜色@1
        // self.tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController(XYHomeTableViewController(), imageNamed: "tabbar_home", title: "首页")
        addChildViewController(XYMessageTableViewController(), imageNamed: "tabbar_message_center", title: "消息")
        addChildViewController(XYDiscoverTableViewController(), imageNamed: "tabbar_discover", title: "发现")
        addChildViewController(XYProfileTableViewController(), imageNamed: "tabbar_profile", title: "我")
        
       
   
    }
    /// 添加子控制器
    ///
    /// - parameter childController: 子控制器
    /// - parameter imageNamed:      子控制器图片名
    /// - parameter title:           子控制器标题
    private func addChildViewController(childController: UIViewController,imageNamed: String,title: String) {
        
        
        let tabBar = XYTabBar()
        // readOnly 的属性不能直接赋值
        // self.tabBar = tabBar
        // 利用KVC间接赋值
        
        self.setValue(tabBar, forKey: "tabBar")
        
        
        
        // 设置文字
//        childController.navigationItem.title = title
//        childController.tabBarItem.title = title
        
        childController.title = title
        
        // 设置图片
        childController.tabBarItem.image = UIImage(named: imageNamed)?.imageWithRenderingMode(.AlwaysOriginal)

        // 设置选中图片
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageNamed)_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        // 统一设置字体颜色@2  也可以每个item设置不同的颜色
        
        let attributs = [NSForegroundColorAttributeName:UIColor.orangeColor()]
        childController.tabBarItem.setTitleTextAttributes(attributs, forState: UIControlState.Selected)
        // 设置item字体的大小
//        let font = [NSFontAttributeName:UIFont.systemFontOfSize(20)]
//        childController.tabBarItem.setTitleTextAttributes(font, forState: UIControlState.Normal)
        // 如果只显示图片,不现实文字(文字包含在图片中).这样可以解决图片靠上的问题
       // childController.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, 0, -5)
        
        addChildViewController(UINavigationController(rootViewController: childController))
        
      //  addChildViewController(childController)
        
    }
    
}













